package net.onceface.application.assembler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.onceface.application.dto.ClientDTO;
import net.onceface.core.domain.Client;


public class ClientAssembler {

	/**
	 * 传入数据库对象和前端传入对象，返回最终修改对象
	 * @param ClientDTO client 数据库对象
	 * @param ClientDTO clientDTO 要修改的数据
	 * **/
	public static void toUpdataDTO(ClientDTO client, ClientDTO clientDTO){
		if(clientDTO.getUserPassword() != null){
			client.setUserPassword(clientDTO.getUserPassword());
		}
		if(clientDTO.getUserAvatar() != null){
			client.setUserAvatar(clientDTO.getUserAvatar());
		}
		if(clientDTO.getNick() != null){
			client.setNick(clientDTO.getNick());
		}
		if(clientDTO.getPerson() != null){
			client.setPerson(PersonAssembler.toUpdataDTO(client.getPerson(), clientDTO.getPerson()));
		}
	}
	
	public static ClientDTO toDTO(Client client){
		if(client == null){
			return null;
		}
		ClientDTO result = new ClientDTO();
		result.setAbolishDate(client.getAbolishDate());
		result.setCreateDate(client.getCreateDate());
		result.setDescription(client.getDescription());
		result.setDisabled(client.isDisabled());
		result.setId(client.getId());
		result.setLastLoginTime(client.getLastLoginTime());
		result.setLastModifyTime(client.getLastModifyTime());
		result.setNick(client.getNick());
		result.setPerson(PersonAssembler.toDTO(client.getPerson()));
		result.setUserAccount(client.getUserAccount());
		result.setUserAvatar(client.getUserAvatar());
		result.setUserDesc(client.getUserDesc());
		result.setUserPassword(client.getUserPassword());
		result.setVersion(client.getVersion());
		return result;
	}
	
	public static Client toEntity(ClientDTO client){
		if(client == null){
			return null;
		}
		Client result = new Client();
		result.setAbolishDate(client.getAbolishDate());
		result.setCreateDate(client.getCreateDate());
		result.setDescription(client.getDescription());
		result.setDisabled(client.isDisabled());
		result.setId(client.getId());
		result.setLastLoginTime(client.getLastLoginTime());
		result.setLastModifyTime(client.getLastModifyTime());
		result.setNick(client.getNick());
		result.setPerson(PersonAssembler.toEntity(client.getPerson()));
		result.setUserAccount(client.getUserAccount());
		result.setUserAvatar(client.getUserAvatar());
		result.setUserDesc(client.getUserDesc());
		result.setUserPassword(client.getUserPassword());
		result.setVersion(client.getVersion());
		return result;
	}
	
	public static List<Client>  toEntities(Collection<ClientDTO>  clients){
		if (clients == null) {
			return null;
		}
		List<Client> results = new ArrayList<Client>();
		for (ClientDTO each : clients) {
			results.add(toEntity(each));
		}
		return results;
	}
	
	public static List<ClientDTO>  toDTOs(Collection<Client>  clients){
		if (clients == null) {
			return null;
		}
		List<ClientDTO> results = new ArrayList<ClientDTO>();
		for (Client each : clients) {
			results.add(toDTO(each));
		}
		return results;
	}
}
